//
//  AddillnessHistoryViewController.m
//  QuickCure1
//
//  Created by 韩春 on 2016/10/31.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "AddillnessHistoryViewController.h"
#import "HCThirdTableViewController.h"

#import "HCThirdDetailTableViewController.h"

#import "NetWorkManager.h"

#import "HCPatientModel.h"


@interface AddillnessHistoryViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UIButton *buttonDeaseName;

@property (weak, nonatomic) IBOutlet UIButton *buttonDeaseDetail;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewPicked;
@property (weak, nonatomic) IBOutlet UITextField *texfPatientName;
@property (weak, nonatomic) IBOutlet UITextField *texfNum;
@property (weak, nonatomic) IBOutlet UITextField *texfTelNum;

@property (weak, nonatomic) IBOutlet UITextView *textViewDesc;

@property (weak, nonatomic) IBOutlet UIImageView *imageViewPatient;

@property(nonatomic,copy)NSString *dateString;
@property(nonatomic,strong)NSArray<NSString *> *dataList;

@property(nonatomic,copy)NSString *currentDease;






//储存疾病类型细分对应的cid
@property (nonatomic, strong) NSArray *cid;

@end


@implementation AddillnessHistoryViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    if (!_diseaseDetails)
    {
        return;
    }
    [_buttonDeaseDetail setTitle:_diseaseDetails forState:UIControlStateNormal];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.title=@"添加病历";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(clickToCommit)];
    
    _buttonDeaseDetail.enabled = NO;
    
    _dataList = @[@"肿瘤",@"血液病",@"心脑血管病",@"神经系统",@"骨科病"];
    
    _cid = @[@(1),@(2),@(3),@(4),@(5)];
    
}

-(void)clickToCommit{
    
    HCPatientModel *model = [[HCPatientModel alloc] init];
    
    model.name = _texfPatientName.text;
    model.number = _texfNum.text;
    model.telNum = _texfTelNum.text;
    model.diseasTypy = _buttonDeaseName.currentTitle;
    model.diseasDetail = _buttonDeaseDetail.currentTitle;
    model.diseasDescription = _textViewDesc.text;
    model.imagePatient = _imageViewPatient.image;
    //获取当前时间
    NSDate *currentDate = [NSDate date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    [dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    
    model.illnessHistoryCurrentTime = dateString;
    
    _dateString = dateString;
    
    
    
    //存储数据
    
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    
    NSLog(@"%@",docPath);
    NSString *filePath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"hc_illnessHistory.plist"]];
    
    //归档@"hc_illnessHistory.plist"
     NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    
    if (array == nil) {
        
       array = [NSMutableArray array];
        
    }
    
    [array insertObject:model atIndex:0];
    
    [NSKeyedArchiver archiveRootObject:array toFile:filePath];
    
//    NSLog(@"%@美妙生活",NSHomeDirectory());
    
    if (_callBackBlock)
    {
        
        _callBackBlock(model);
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}



- (IBAction)selectillnessType:(id)sender {

    HCThirdTableViewController *thirdVC = [[HCThirdTableViewController alloc] init];
    
    thirdVC.CallBackBlock = ^(NSString *deaseName,NSInteger index){
        
        [_buttonDeaseName setTitle:deaseName forState:UIControlStateNormal];
        
        _buttonDeaseDetail.enabled = YES;
        
        _index = index;
        
        _currentDease = deaseName;
        
    };
    
    thirdVC.dataList = [_dataList mutableCopy];
    
    [self.navigationController pushViewController:thirdVC animated:YES];
}

- (IBAction)selectillnessDetails:(UIButton *)sender {
    
    HCThirdDetailTableViewController *thirdVC = [[HCThirdDetailTableViewController alloc] init];
    
    thirdVC.index = _index;
    
    [self.navigationController pushViewController:thirdVC animated:YES];

    
    
}

- (IBAction)clickToSaveImage:(UIButton *)sender {
    
    UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
    
    pickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    pickerVC.delegate = self;
    
    [self presentViewController:pickerVC animated:YES completion:nil];
    
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    _imageViewPicked.image = info[UIImagePickerControllerOriginalImage];
    
    [picker dismissViewControllerAnimated:YES completion:nil];

}




@end
